/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.collablet.coop.recommend;

import java.util.List;

import net.vidageek.mirror.dsl.Mirror;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.core.framework.Collablet;

@RequestScoped
@Resource
public class RecommendMgrController {

    private final Result result;

    public RecommendMgrController(Result result) {
        this.result = result;
    }

    @Get
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}")
    public void list(final RecommendMgrInstance recommendMgr) {
        result.include("recommendMgr", recommendMgr);
    }

    @Post
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}")
    public void save(final RecommendMgrInstance recommendMgr, Recommend recommend) {
        recommendMgr.saveRecommend(recommend);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(recommendMgr);
    }

    @Get
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}/configure")
    public void listConfigure(RecommendMgrInstance recommendMgr) {
        result.include("numRelated", recommendMgr.getNumRelated());
        result.include("recommendMgr", recommendMgr);
    }

    @Post
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}/updateNumRelated")
    public void setNumRelated(RecommendMgrInstance recommendMgr, int max) {
        recommendMgr.setNumRelated(max);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(recommendMgr);
    }

    @Get
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}/calcRecommend")
    public void recalcRecommend(RecommendMgrInstance recommendMgr) {
        // FIXME: NÃO É PARA FAZER ISSO. Esse código supõe que o método "list()" existe no collablet. Java é fortemente tipado por uma boa razão.
        List<?> list = (List<?>) new Mirror().on(Collablet.findByName("AUN").getBusinessObject()).invoke().method("list").withoutArgs();
        recommendMgr.calculeAll(list);

        result.include("recommendMgr", recommendMgr);
        result.include("finish", "true");
        result.use(Results.referer()).redirect();
    }

    @Post
    @Path(value="/groupware-workbench/recommend/{recommendMgr}/searchRelated/{id}")
    public void generateRecommend(RecommendMgrInstance recommendMgr, List<Object> others, Object entity) {
        recommendMgr.generateRelated(others, entity);
    }

    @Get
    @Path(value = "/groupware-workbench/recommend/{recommendMgr}/configureMethod")
    public void configureMethod(RecommendMgrInstance recommendMgr) {
        result.include("classes", recommendMgr.listClasses());
        result.include("recommendMgr", recommendMgr);
    }

    @Post
    @Path(value="/groupware-workbench/recommend/{recommendMgr}/updateMethodsEnable")
    public void updateMethodsEnable(final RecommendMgrInstance recommendMgr, List<String> enabledMethodList) {

        recommendMgr.setEnableds(enabledMethodList);

        result.include("recommendMgr", recommendMgr);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(recommendMgr);
    }
}
